<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\XcSiteOrder;
use App\Models\XcUserConsumerDetails;
use App\Models\XcUserRecharge;
use Illuminate\Http\Request;
use App\Models\XcUser;
use App\Models\XcUserXiche;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use function foo\func;

/**
 * 用户管理
 *
 */
class XcUserManagementController extends Controller
{

	public $res = ['code' => -1, 'msg' => 'failed'];
	/**
     * 用户订单：通过用户ID获得订单列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function siteorderlist(Request $request)
    {
        $id = $request->get("id");
        try{
            $user = XcUserXiche::where("id", $id)->first();
            $result = XcSiteOrder::where(['openid' => $user->openid, 'status' => 1])->orderBy('id','desc')->paginate($request->get('limit',10))->toArray();
            foreach ($result['data'] as &$val) {
                $val['status_text'] = $val['status'] == 2 ? '已退款' : ($val['status'] == 1 ? '已使用' : '未使用');
            }
            $this->res['code'] = 0;
            $this->res['data'] = $result;
            return response()->json($this->res);
        }catch (\Exception $e){
            return response()->json(['msg' => $e->getMessage()]);
        }
    }

    /**
     * 充值订单
     * 
     */ 
    public function rechargelist(Request $request)
    {
    	return response()->json($this->res);
    }

    /**
     * 自助洗车机全部订单,菜单：我的>管理员>全部订单
     * 
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */ 
    public function orderlistall(Request $request)
    {
        //获得类型名称
        function getPayType($type, $money)
        {
            if ($type == 6) {
                $pay_type = '微信支付';
            } elseif ($type == 0) {
                $pay_type = $money > 0 ? '余额支付' : '积分支付';
            } elseif ($type == 5) {
                $pay_type = '次卡支付';
            } else {
                $pay_type = '会员卡支付';
            }
            return $pay_type;
        }

        $date = $request->get("date", "");
        $site_id = $request->get("site_id", "");
        $keyword = $request->get("keyword", "");

        try{

            $query = XcSiteOrder::where('status', 1);

            if (!empty($date)) $query = $query->whereRaw("SUBSTRING(created_at,1,10) = '$date'");
            if ($site_id) $query = $query->where('site_id', $site_id);
            if (!empty($keyword)) {
                $uid = XcUser::where('mobile', $keyword)->value('id');
                $query = $query->where('user_id', $uid);
            }
            $query = $query->with('user');
            $list = $query->orderBy('id', 'desc')->paginate($request->get('limit', 10))->toArray();

            //整理数据
            foreach ($list['data'] as &$va) {
                $va['pay_type'] = getPayType($va['type'], $va['money']);
                if (empty($va['mobile'])){
                    $va['mobile'] = $va['user']['mobile'];
                }
                unset($va['user']);
            }

            $this->res['code'] = 0;
            $this->res['msg'] = 'success';
            $this->res['data'] = $list;
            return response()->json($this->res);
        }catch (\Exception $e){
            return response()->json(['msg' => $e->getMessage()]);
        }
    }

    /**
     * 我的消费订单，菜单：我的>我的订单>消费订单
     * 
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */ 
    public function mysiteorder(Request $request)
    {
        $api_token = $request->get("api_token");
        try{
            $user = XcUser::where("api_token", $api_token)->first();
            $list = XcSiteOrder::where([["openid", '=', $user->openid],['status', '=', 1]])->paginate($request->get('limit', 10))->toArray();
            $this->res['code'] = 0;
            $this->res['msg'] = 'success';
            $this->res['data'] = $list['data'];
            return response()->json($this->res);
        }catch (\Exception $e){
            return response()->json(['msg' => $e->getMessage()]);
        }
    }






}

?>